<template>
  <div id="desk3">
    <h3>购物车</h3>
   <el-table :data="List" stripe @selection-change="selectionChange">
     <el-table-column type="selection"></el-table-column>
     <el-table-column label="商品图片">
       <template #default="scope">
          <img :src="scope.row.commodityPictureList[0].image" alt=""
               style="width: 50px; height: 50px">
       </template>
     </el-table-column>
     <el-table-column label="商品名称" prop="commodity.commodityName"></el-table-column>
     <el-table-column label="单价">
       <template #default="scope">
          {{scope.row.commodity.commodityPrice.toFixed(2)}}
       </template>
     </el-table-column>
     <el-table-column label="数量">
       <template #default="scope">
         <el-input-number v-model="scope.row.quantity"
                          @change="value =>quantityChange(value,scope.row.cartId)"
                          :min="1"
         ></el-input-number>
       </template>
     </el-table-column>
     <el-table-column label="小计">
       <template #default="scope">
         {{(scope.row.commodity.commodityPrice*scope.row.quantity).toFixed(2)}}
       </template>
     </el-table-column>
     <el-table-column label="操作">
       <template #default="scope">
          <el-button @click="remove(scope.row.cartId)" type="danger" size="mini">移出</el-button>
       </template>
     </el-table-column>
   </el-table>
    <div style="text-align: right;margin-top: 20px">
      <span>总计: {{totalPrice}}元</span>
    </div>
    <el-button style="margin: auto;" @click="checkout" :disabled="selectedList.length === 0" type="primary">结算</el-button>
  </div>
</template>
<script>
export default{
  name:"desk3",
  data(){
    return{
      List:[],
      //页号
      pageNo:1,
      //页大小
      pageSize:4,
      //多选
      selectedList:[]
    }
  },
//计算属性
  computed:{
    totalPrice(){
        return this.selectedList.reduce((total,item)=>{
          return total += item.commodity.commodityPrice * item.quantity
        },0)
    }
  },
  methods:{
    //查询购物车
    getList(){
      let uid=window.sessionStorage.getItem("userId1")
      console.log("uid",uid)
      if(this.$root.admin2 === null||this.$root.admin2===''||this.$root.admin2==='null'){
        alert("请先登账号再进行操作")
        this.$router.push("/Login")
      }else {
      this.$axios.get(`ShoppingCart/All/${this.pageNo}/${this.pageSize}/${uid}`).then(r=>{
        this.List=r.data.rows
        console.log("this.List",r.data.rows)
      })}
    },
    //修改数量
    quantityChange(num,cartId){
      this.$axios.put(`ShoppingCart/${cartId}/${num}`).then(r=>{
        if(r.data==="success"){
          this.$message.success("修改成功")
        }else {
          this.$message.error("修改失败")
        }
      })
    },
    //删除单个商品
    remove(cartId){
      if(confirm("确认要删除吗？")) {
        this.$axios.delete(`ShoppingCart/${cartId}`).then(r=>{
        if(r.data==="success"){
          this.$message.success("删除成功")
          this.getList()
        }else {
          this.$message.error("删除失败")
        }

      })}

    },
    //结算
    checkout(){
      // 路由跳转，跳转到订单确认页面
      this.$router.push({name:`CheckOut`,params:{cartList:this.selectedList}})
    },
    //多选
    selectionChange(val){
      this.selectedList=val
      console.log("this.selectedList",this.selectedList)
    }
  },
  created() {
  this.getList()
  }
}
</script>
<style scoped>
#desk3{
  padding: 30px 100px;
}
</style>